package com.spt.springbootzhsx.service;

import com.spt.springbootzhsx.dao.PlanDao;
import com.spt.springbootzhsx.entity.Plan;
import com.spt.springbootzhsx.entity.back.PlanBack;
import com.spt.springbootzhsx.mapper.PlanMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@Service
public class PlanImpl implements PlanService{
    @Autowired
    private PlanDao planDao;
    @Autowired
    private PlanMapper planMapper;

    @Override
    public ArrayList<PlanBack> getAllPlan() {
        return planDao.getAllPlan();
    }

    @Override
    public ArrayList<PlanBack> getAllPlanNow() {
        return planDao.getAllPlanNow();
    }

    @Override
    public ArrayList<PlanBack> getPlanByMname(String mname) {
        return planDao.getPlanByMname(mname);
    }

    @Override
    public ArrayList<PlanBack> getPlanByMnameNow(String mname) {
        return planDao.getPlanByMnameNow(mname);
    }

    @Override   //按照电影名或者影厅搜索场次
    public ArrayList<PlanBack> getPlanByMnameOrCname(String key) {
        ArrayList<PlanBack> mlist=planDao.getPlanByMname(key);
        if(mlist.size()>0){
            //如果按照电影名搜索有结果
            return mlist;
        }
        //否则按照影厅搜索
        ArrayList<PlanBack> clist=planDao.getPlanByCname(key);
        return clist;
    }

    @Override  //按照电影名或者影厅搜索当前时间以后的场次
    public ArrayList<PlanBack> getPlanByMnameOrCnameNow(String key) {
        ArrayList<PlanBack> mlist=planDao.getPlanByMnameNow(key);
        if(mlist.size()>0){
            //如果按照电影名搜索有结果
            return mlist;
        }
        //否则按照影厅搜索
        ArrayList<PlanBack> clist=planDao.getPlanByCnameNow(key);
        return clist;
    }

    @Override
    public ArrayList<PlanBack> getPlanByMidAndDate(Integer mid, long date) {
        Date date1=new Date(date);
        ArrayList<PlanBack> list=new ArrayList<>();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        String time=simpleDateFormat.format(date1);
        try {
            Date st=simpleDateFormat.parse(time);
            list=planDao.getPlanByMidAndDate(mid,st, new Date(st.getTime()+24*60*60*1000));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public PlanBack getPlanByPid(Integer pid) {
        return planDao.getPlanByPid(pid);
    }


    @Override
    public String addPlan(Plan plan) {
        int k=planMapper.addPlan(plan);
        if(k>=1){
            return "排片添加成功！";
        }else{
            return "排片添加失败！";
        }
    }

    @Override
    public String updatePlan(Plan plan) {
        int k=planMapper.updatePlan(plan);
        if(k>=1){
            return "排片更新成功！";
        }else{
            return "排片更行失败！";
        }
    }

    @Override
    public String deletePlan(Integer pid) {
        int k=planMapper.deletePlan(pid);
        if(k>=1){
            return "排片删除成功！";
        }else{
            return "排片删除失败！";
        }
    }
}
